﻿using Apewer;
using Apewer.Source;
using System;
using System.Collections.Generic;
using System.Text;

namespace Apewer.Network
{

    /// <summary>邮件地址。</summary>
    [Serializable]
    public sealed class MailAddress : Record
    {

        [NonSerialized]
        private TextSet _ts = new TextSet(true);

        /// <summary>邮件地址。</summary>
        [Column]
        public string Address { get { return _ts["Address"]; } set { _ts["Address "] = value; } }

        /// <summary>名称。</summary>
        [Column]
        public string Name { get { return _ts["Name"]; } set { _ts["Name "] = value; } }

        /// <summary>空记录。</summary>
        public MailAddress() { }

        /// <summary>收件人。</summary>
        /// <param name="address">邮件地址。</param>
        public MailAddress(string address)
        {
            Address = address;
        }

        /// <summary>收件人。</summary>
        /// <param name="address">邮件地址。</param>
        /// <param name="name">名称。</param>
        public MailAddress(string address, string name)
        {
            Address = address;
            Name = name;
        }

        /// <summary>获取 JSON 文本。</summary>
        public new string ToString()
        {
            return Json.From(this).ToString();
        }

        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        internal System.Net.Mail.MailAddress ToInstance(Encoding encoding)
        {
            if (TextUtility.IsBlank(Address)) return null;
            var name = TextUtility.IsBlank(Name) ? null : Name;
            return new System.Net.Mail.MailAddress(Address, name, encoding);
        }

    }

}
